﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;

namespace FeedTheWalrus
{

    public class Walrus
    {
        public static Random fishes; //selects the color of fish to eat
        public static Color[] colors; // stores the colors for the walrus to eat;
        public int targetFish; // the fish to eat
        TimeSpan nextSwitch; // time until walrus switches what it wants to eat
        TimeSpan totalSwitch; // total amount of time between switches

        Vector2 position = Vector2.Zero;

        ThunderFish.Sprite walrus;
        ThunderFish.SpriteSheet walrusSpriteSheet;

        ThunderFish.Sprite flag;
        ThunderFish.SpriteSheet flagSheet;

        public static int FishColorCount { get { return colors.Length; } }


        public Walrus()
        {
            colors = new Color[3];
            colors[0] = Color.Red;
            colors[1] = Color.Blue;
            colors[2] = Color.Green;

            

        }

        public void setFishColor()
        {
            targetFish = fishes.Next(0, colors.Length - 1);
        }

        public void OnNavigatedTo(ContentManager content)
        {
            //intialize, load
            nextSwitch = new TimeSpan(0, 0, 7);
            totalSwitch = new TimeSpan(0, 0, 7);
            fishes = new Random();

            walrusSpriteSheet = new ThunderFish.SpriteSheet(content.Load<Texture2D>("walrus"), 2, 1);
            walrus = new ThunderFish.Sprite(walrusSpriteSheet);

            flagSheet = new ThunderFish.SpriteSheet(content.Load<Texture2D>("flag"), 2, 1);
            flag = new ThunderFish.Sprite(flagSheet);

            flag.LoopAnimation();
            flag.FrameLength = new TimeSpan(0, 0, 0, 0, 400);

            walrus.FrameLength = new TimeSpan(0, 0, 0, 0, 600);
            walrus.PlayAnimation();
        }

        public void OnUpdate(GameTimerEventArgs e)
        {
            nextSwitch -= e.ElapsedTime;

            walrus.Update(e.ElapsedTime);
            flag.Update(e.ElapsedTime);

            if (nextSwitch <= TimeSpan.Zero)
            {
                nextSwitch = new TimeSpan(0, 0, fishes.Next(0, 4) + 4);
                targetFish = fishes.Next(0, colors.Length);
                walrus.PlayAnimation();
            }

        }


        public void OnDraw(SpriteBatch sb, GameTimerEventArgs e)
        {
            if (walrus.CurrentFrame == 1)
                flag.Draw(sb,
                    Vector2.Zero,
                    colors[targetFish]);
            walrus.Draw(sb);
        }

        public void OnNavigatedFrom()
        {
            //unitialize
        }

    }
}
